const API_BASE_URL = "http://localhost:8081";  // ✅ 确保带上 8081 端口
const request = (url, method, data = {}, auth = true) => {
  const token = wx.getStorageSync('token');
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${API_BASE_URL}${url}`,
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
        'Authorization': auth ? `Bearer ${token}` : ''
      },
      success: res => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          wx.showToast({ title: res.data.message || "请求失败", icon: "none" });
          reject(res);
        }
      },
      fail: err => {
        wx.showToast({ title: "网络错误", icon: "none" });
        reject(err);
      }
    });
  });
};

const api = {
  request
};

module.exports = api;  // ✅ 确保正确导出 `api`
console.log("api.js 加载成功");
